//
//  CellOfNews.m
//  UI12_01_UITableView自定义Cell
//
//  Created by Scott on 16/3/24.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "CellOfNews.h"
#import "ModelOfNews.h"

#define WIDTH self.contentView.frame.size.width

#define HEIGHT self.contentView.frame.size.height



@interface CellOfNews ()

// 子控件声明为属性
@property (nonatomic, retain) UILabel *labelOfTitle;
@property (nonatomic, retain) UILabel *labelOfDate;
@property (nonatomic, retain) UILabel *labelOfSummary;
@property (nonatomic, retain) UIImageView *imageViewOfPic;

@end

@implementation CellOfNews

- (void)dealloc {
    [_labelOfDate release];
    [_labelOfSummary release];
    [_labelOfTitle release];
    [_imageViewOfPic release];
    [super dealloc];
}


#pragma mark - override init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        // 创建子控件.
        [self createSubViews];
        
    }
    
    return self;
}

- (void)createSubViews {
    
    self.imageViewOfPic = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imageViewOfPic];
    [_imageViewOfPic release];
    
    
    self.labelOfTitle = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfTitle];
    [_labelOfTitle release];
    
    
    self.labelOfDate = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfDate];
    [_labelOfDate release];
    
    self.labelOfDate.textAlignment = NSTextAlignmentRight;
    
    
    self.labelOfSummary = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfSummary];
    [_labelOfSummary release];
    
    self.labelOfSummary.numberOfLines = 0;
    
    
//    self.imageViewOfPic.backgroundColor = [UIColor redColor];
//    self.labelOfTitle.backgroundColor = [UIColor redColor];
//    self.labelOfDate.backgroundColor = [UIColor redColor];
//    self.labelOfSummary.backgroundColor = [UIColor redColor];
}


#pragma mark - override layoutSubviews
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    // 设置子控件frame属性.
    self.imageViewOfPic.frame = CGRectMake(5, 5, 85 * 4 / 3.0, 85);
    
    self.labelOfTitle.frame = CGRectMake(85 * 4 / 3.0 + 15, 5, WIDTH - 20 - 85 * 4 / 3.0, 40);
    
    self.labelOfDate.frame = CGRectMake(WIDTH - 155, 50, 150, 40);
    
    
    CGFloat height = [CellOfNews heightOfCellNews:self.labelOfSummary.text];
    
    self.labelOfSummary.frame = CGRectMake(5, 95, WIDTH - 10, height - 85 - 15);

}

#pragma mark - 子控件赋值
- (void)setCellContentValues:(ModelOfNews *)model {
    
    // 子控件赋值.
    self.imageViewOfPic.image = [UIImage imageNamed:@"1.jpg"];
    
    
    self.labelOfTitle.text = model.title;
    
    
    
    self.labelOfDate.text = [self dateFormatReverseWithTimeInterval:model.lastUpdateTime];
    

    self.labelOfSummary.text = model.summary;
    
    
}

/** 时间格式的转化. 核心API: NSDate */
- (NSString *)dateFormatReverseWithTimeInterval:(NSString *)timeInterval {
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval.doubleValue];
    
    // 格式转化.
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd  HH:mm"];

    NSString *formattedDateString = [dateFormatter stringFromDate:date];
    
    return formattedDateString;
    
}

#pragma mark - 计算高度
+ (CGFloat)heightOfCellNews:(NSString *)text {
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 10;
    
    // 计算新的矩形
    // 参数1: 预估大小
    // 参数2: 绘图选项
    // 参数3: 文字属性
    // 参数4: 绘制上下文
    CGRect newRect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];

    
    
    return newRect.size.height + 85 + 15;
    
}



#pragma mark - other
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
